<?php

namespace App\Http\Controllers;

use App\BGoods;
use App\Good;
use App\Goods;
use App\Goodsfirst;
use App\Goodssecond;
use App\GoodsTask;
use App\Order;
use App\Ordertask;
use App\Task;
use App\Work;
use App\Worked;
use App\Worktask;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Rule;

class AllotController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }
    public function index()
    {
        $uid=\Auth::id();
        $rules=Rule::where('uid',$uid)->get();
        $date=Carbon::now();
        $date=date('Y-m-d',strtotime($date) );
        $worktasks=Worktask::where('date',$date)->whereNotNull('workerName')->get();
        $goodsTasks=GoodsTask::where('date',$date)->get();

        return view('work.allot',['worktasks'=>$worktasks,'goodsTasks'=>$goodsTasks,'date'=>$date,'rules'=>$rules]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function allotView($date)
    {
        if($date != null){
            $date=$date;
        }else{
            $date=Carbon::now();
        }
        $date=date('Y-m-d',strtotime($date) );
        return view('work.allot',['date'=>$date]);

    }
   

    public function add(Request $request)
    {

        $add=$request->only(['orderTaskId','workId','output']);
        $orderTaks=Ordertask::find($request->input('orderTaskId'));
        $last=$orderTaks->output-$orderTaks->alloted;
//        if($add['output'] > $last){
//            dd($add['output']);
//        }

        $add['goodsName']=$orderTaks->goodsName;
        $add['goodsId']=$orderTaks->goodsId;
        $work=Work::find($request->input('workId'));
        $work->alloted=1;
        $work->save();
        $add['date']=$work->date;
        $add['workerId']=$work->workerId;
        $count=Task::where('orderTaskId',$add['orderTaskId'])->where('workId',$add['workId'])->count();
        if($count > 0){
            $task=Task::where('orderTaskId',$add['orderTaskId'])->where('workId',$add['workId'])->first();
            $task->output=$task->output+$add['output'];
            $task->save();
        }else{
            Task::create($add);
        }

        $orderTaks->alloted=Task::where('orderTaskId',$request->input('orderTaskId'))->sum('output');
        //dd($orderTaks->alloted);
        $orderTaks->save();
        $order=Order::find($orderTaks->orderId);
        $order->alloted=Ordertask::where('orderId',$orderTaks->orderId)->sum('alloted');
        $order->save();

    }

    public function allot($date,$orderId,$allotNum)
    {
        if($date != null){
            $date=$date;
        }else{
            $date=Carbon::now();
        }
        $date=date('Y-m-d',strtotime($date) );
        $order=Order::find($orderId);
        $goodId=$order->goodsId;
        $goodsfirst=Good::find($goodId);

        $seconds = $goodsfirst->children()->get();

        foreach ($seconds as $second){
            $addGoodsTask['orderId']=$orderId;
            $addGoodsTask['date']=$date;
            $addGoodsTask['goodsId']=$second->id;
            $addGoodsTask['goodsName']=$second->name;
            $addGoodsTask['goodsOutput']=$allotNum;
            $addGoodsTask['goodsAllotted']=0;
            GoodsTask::create($addGoodsTask);
        }

        return view('work.allot',['date'=>$date]);

    }

    public function getOrders(Request $request)
    {
            $orders=Order::all();
            return response()->json($orders);

    }

    public function allotWorker(Request $request)
    {
        $date=$request->input('date');
        $date=date('Y-m-d',strtotime($date) );
        $goodsTaskId=$request->input('selectGoods');
        $workTaskId=$request->input('selectWorkTask');
        $output=$request->input('allotWorkerNum');
        $goodsTask= GoodsTask::find($goodsTaskId);
        $goodsName=$goodsTask->goodsName;
        $goodsAlloted=$goodsTask->goodsAllotted;

        $allotOrNot=Worktask::where('id',$workTaskId)->whereNotNull('goodsTaskId')->count();

        if($allotOrNot == 0){
            Worktask::where('id',$workTaskId)->where('date',$date)->update(['goodsTaskId'=>$goodsTaskId,'output'=>$output,'goodsName'=>$goodsName]);
            $newOutput=$goodsAlloted+$output;
            GoodsTask::where('id',$goodsTaskId)->update(['goodsAllotted'=>$newOutput]);
            $worktasks=Worktask::where('date',$date)->whereNotNull('workerName')->get();
            return response()->json($worktasks);
        }elseif ($allotOrNot == 1){
            $oldGoodsTask=Worktask::find($workTaskId);
            $oldGoodsTaskId=$oldGoodsTask->goodsTaskId;
            $oldGoodsTaskOutput=$oldGoodsTask->output;
            $oldGoodsTask=GoodsTask::find($oldGoodsTaskId);

            $oldOutput=$oldGoodsTask->goodsAllotted-$oldGoodsTaskOutput;
            GoodsTask::where('id',$oldGoodsTaskId)->update(['goodsAllotted'=>$oldOutput]);

            Worktask::where('id',$workTaskId)->where('date',$date)->update(['goodsTaskId'=>$goodsTaskId,'output'=>$output,'goodsName'=>$goodsName]);
            $newOutput=$output+$goodsAlloted;
            GoodsTask::where('id',$goodsTaskId)->update(['goodsAllotted'=>$newOutput]);
            $worktasks=Worktask::where('date',$date)->whereNotNull('workerName')->get();
            return response()->json($worktasks);
        }



    }

    public function getGoodsTask($date)
    {

        $date=date('Y-m-d',strtotime($date) );
        $goodsTasks=GoodsTask::where('date',$date)->get();
        return response()->json($goodsTasks);

    }

    public function getWorks(Request $request)
    {
        $date=$request->input('date');
        $date=date('Y-m-d',strtotime($date) );
        $works=Work::where('date',$date)->whereNotNull('workerName')->get();
        return response()->json($works);

    }

}
